package com.chinasofti.xml;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import java.io.File;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;

public class XmlParse {
    public static void main(String[] args) throws DocumentException {
        //1.获取解析器对象
        SAXReader saxReader =new SAXReader();
        //2/利用解析器把xml文件加载到内存中，并返回一个对象文档
        Document document =saxReader.read(new File("java426\\xml\\student.xml"));
        //3.获取跟标签
        Element rootElement = document.getRootElement();
        // System.out.println(rootElement.getName());
        //4.通过跟标签获取是studen标签
        List<Element> studentElements = rootElement.elements();
        //System.out.println(studentElements.size());
        //5.获取每一个学生标签
        ArrayList<Student> list = new ArrayList<>();//装学生的集合
        for (Element element:studentElements) {
            //获取id属性及值
            Attribute attribute = element.attribute("id");
            String id = attribute.getValue();
            //获取标签名 name 及内容
            Element nameelement = element.element("name");
            String name = nameelement.getText();
            //获取标签名 age 及内容
            Element agelement = element.element("age");
            String age =agelement.getText();
            //System.out.println(id+"----"+name+"----"+age);
            Student student = new Student(id,name,Integer.parseInt(age));
            list.add(student);
        }
        for (Student student :list){
            System.out.println(student);
    }
    }
}
                             